package cn.kai.ums.controller;

import cn.kai.ums.mapper.UmsGradeMapper;
import cn.kai.ums.model.UmsGrade;
import cn.kai.ums.service.UmsGradeService;
import cn.kai.ums.tools.JsonData;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("ums/v1/grade")
@Api(tags = "成绩控制类")
public class GradeController {

    @Autowired
    private UmsGradeService umsGradeService;

    @Autowired
    private UmsGradeMapper umsGradeMapper;

    @ApiOperation("根据学号和课程号模糊查询成绩")
    @GetMapping
    public JsonData getGradeList(@RequestParam(defaultValue = "1",required = false) Integer pageNum
            ,@RequestParam(defaultValue = "5",required = false) Integer pageSize
            ,@RequestParam(defaultValue = "",value = "likegrade",required = false) String likegrade){

        Page<UmsGrade> umsGradePage = umsGradeMapper.selectPage(new Page<>(pageNum,pageSize)
                , Wrappers.<UmsGrade>lambdaQuery()
                        .like(UmsGrade::getGradeStudentNo,likegrade)
                        .or()
                        .like(UmsGrade::getGradeCourseNo,likegrade));

        return JsonData.buildSuccess(umsGradePage);
    }

    @PostMapping
    public JsonData saveGrade(@RequestBody UmsGrade umsGrade){
        return JsonData.buildSuccess(umsGradeService.save(umsGrade));
    }

    @PutMapping
    public JsonData updateGrade(@RequestBody UmsGrade umsGrade){
        return JsonData.buildSuccess(umsGradeService.updateById(umsGrade));
    }

    @DeleteMapping
    public JsonData delGrade(@RequestParam Integer gid){
        return JsonData.buildSuccess(umsGradeService.removeById(gid));
    }

    @PostMapping("/delBatchGrade")
    public JsonData delBatchGrade(@RequestBody List<Integer> gids){
        return JsonData.buildSuccess(umsGradeService.removeByIds(gids));
    }
}
